/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：ViewScreen.java <br>
 * 创建时间：2013-8-5 下午1:43:22 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.battle;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.Assets;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.netbattle.NetworkMode;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.ui.NotificationScreen;

/**
 * 类名：ViewScreen <br>
 * 说明：显示屏
 *
 * @author 12548
 */
public class ViewScreen extends CScreen {

	int hit = 0;

	private String str;

	public static final float LAST_TIME = 1.4f;
	static float time = 0;

	volatile boolean posting = false;

	/**
	 * @param instance
	 */
	public ViewScreen(String text) {
		super();
		Logger.w(text);
		str = text;
		time = 0;
//		BasicScreen.recheck();
	}


	/**
	 * @param instance
	 */
	public ViewScreen(Game game, int hit, String text) {
		super();
		Logger.w(text);
		time = 0;
		this.hit = hit;
		str = text;
//		BasicScreen.recheck();
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {
		if (posting) return;
		time += deltaTime;
		if (time >= LAST_TIME) {
			Logger.w(time + "");
			time = 0;
//			game.setScreen(Gmud.bs);
			getGame().popScreen();
		}

//		if(text.endsWith("#hit1#")) {
//			setText(text.substring(0, text.length()-6));
//			setHit(1);
//		}
//		else if(text.endsWith("#hit2#")) {
//			setText(text.substring(0, text.length()-6));
//			setHit(2);
//		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#present(float)
	 */
	@Override
	public void present(float deltaTime) {
		BattleScreen.getInstance().present(deltaTime);
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.drawRect(64, 98, 192, 96, Gmud.getBgColor());
		g.drawText(str, 64, 98, FontSize.FT_16PX, 190);
		if (hit == 1) {
			g.drawPixmap(Assets.boom, 32 * 7 + 10, 42);
		} else if (hit == 2) {
			g.drawPixmap(Assets.boom, 74, 42);
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#pause()
	 */
	@Override
	public void pause() {

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#resume()
	 */
	@Override
	public void resume() {

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#dispose()
	 */
	@Override
	public void dispose() {

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#isStable()
	 */
	@Override
	public boolean isStable() {
		return true;
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#onInit()
	 */
	@Override
	public void onInit() {

		if (BattleScreen.getInstance().networkMode == NetworkMode.HOST) {
			RequestParams params = GmudHttp.initBattleParams();
			params.addBodyParameter("stateinfo", new Gson().toJson(BattleStateInfo.getState()));
			params.addBodyParameter("msg", str);
			params.addBodyParameter("hit", hit + "");

			posting = true;
			new GmudHttp("正在发送数据……").postBackground(GmudRestUrl.HostPostMsg, params, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					JSONObject jo;
					try {
						jo = new JSONObject(info.result);
						String s = jo.getString(GmudHttp.KEY_STATE);
						if (!s.equals(GmudHttp.STATE_OK)) {
							getGame().popToRootScreen();
							new NotificationScreen("已断线！").pushToGame();
						} else {
							posting = false;
						}
					} catch (JSONException e) {
						getGame().popToRootScreen();
						new NotificationScreen("已断线！").pushToGame();
					}

				}

				@Override
				public void onFail(HttpException error, String msg) {
					getGame().popToRootScreen();
					new NotificationScreen("已断线！").pushToGame();
				}
			});
		}

	}


}
